
*Set the root to the directory: 
global root "/Volumes/PortableSSD/Electricvehicles/ReplicationPackage" 

**This code creates the summary statistics tables 1 and 2 as well as the appendix tables A1:

*Set the working directory to store the results directly where they are needed:
cd "$root/Results/tables_graphs"

**Create Table 1: 

use "$root/Data/Produced/EV_welfare04.dta", clear

preserve 

gcollapse (mean) typkw car_size price height weight drive_cost car_tax  (lastnm) drivetype env_friendly , by(car_option)


matrix Tab1_tbl = J(17,9,.)

local rowcount=2 
local colcount=1

su car_option 

matrix Tab1_tbl[`rowcount',`colcount'] = r(N)
local rowcount=`rowcount'+1

forvalues i=1/4 {

su car_option if drivetype==`i'	
matrix Tab1_tbl[`rowcount',`colcount'] = r(N)
local rowcount=`rowcount'+1

}

local rowcount=2
local colcount=2
foreach var of varlist price car_tax typkw weight height car_size drive_cost env_friendly {
	
	sum `var'
	matrix Tab1_tbl[`rowcount',`colcount'] = r(mean)
	local colcount=`colcount'+1
	
}

local rowcount=`rowcount'+1
local colcount=2

forvalues i=1/4 {
foreach var of varlist price car_tax typkw weight height car_size drive_cost env_friendly {
	
	sum `var' if drivetype==`i'
	matrix Tab1_tbl[`rowcount',`colcount'] = r(mean)
	local colcount=`colcount'+1
	
}
local rowcount=`rowcount'+1
local colcount=2

}


restore

preserve 

keep if choice==1

local rowcount=8 
local colcount=1

su car_option 

matrix Tab1_tbl[`rowcount',`colcount'] = r(N)
local rowcount=`rowcount'+1

forvalues i=1/4 {

su car_option if drivetype==`i'	
matrix Tab1_tbl[`rowcount',`colcount'] = r(N)
local rowcount=`rowcount'+1

}

local rowcount=8
local colcount=2
foreach var of varlist price car_tax typkw weight height car_size drive_cost env_friendly {
	
	sum `var'
	matrix Tab1_tbl[`rowcount',`colcount'] = r(mean)
	local colcount=`colcount'+1
	
}

local rowcount=`rowcount'+1
local colcount=2

forvalues i=1/4 {
foreach var of varlist price car_tax typkw weight height car_size drive_cost env_friendly {
	
	sum `var' if drivetype==`i'
	matrix Tab1_tbl[`rowcount',`colcount'] = r(mean)
	local colcount=`colcount'+1
	
}
local rowcount=`rowcount'+1
local colcount=2

}

restore 



preserve 

egen tot_choice=sum(choice), by(car_option)

egen max_choice=max(tot_choice), by(drivetype)
keep if max_choice==tot_choice
keep if choice==1


local rowcount=14 
local colcount=1


forvalues i=1/4 {

su car_option if drivetype==`i'	
matrix Tab1_tbl[`rowcount',`colcount'] = r(N)
local rowcount=`rowcount'+1

}

local rowcount=14
local colcount=2

forvalues i=1/4 {
foreach var of varlist price car_tax typkw weight height car_size drive_cost env_friendly {
	
	sum `var' if drivetype==`i'
	matrix Tab1_tbl[`rowcount',`colcount'] = r(mean)
	local colcount=`colcount'+1
	
}
local rowcount=`rowcount'+1
local colcount=2

}

restore 



matrix colnames Tab1_tbl =  N price tax kw weight height size drivecost env_friend 
matrix rownames Tab1_tbl =  PanA tot_set gas_set diesel_set ev_set hy_set PanB tot_obs gas_obs diesel_obs ev_obs hy_obs PanC gas_most diesel_most ev_most hy_most   

foreach num of numlist 1(1)17 {
foreach numb of numlist 1(1)5 {
matrix Tab1_tbl[`num',`numb']=round(Tab1_tbl[`num',`numb'],1)
}
}

foreach num of numlist 1(1)17 {
foreach numb of numlist 6(1)9 {
matrix Tab1_tbl[`num',`numb']=round(Tab1_tbl[`num',`numb'],.01)
}
}

matrix list Tab1_tbl

estadd matrix Tab1_tbl, replace
#delim ;
estout matrix(Tab1_tbl, fmt(%9.0gc)) using "main/car_sum_stats.tex", 
style(tex)  mlabels(,none) cells(e(Tab1_tbl)(fmt(%9.2f)))
varlabels (  PanA "\textit{Choice Set}"
			tot_set "Total"
			gas_set "Gasoline (49.5\%)"
			diesel_set "Diesel (35.4\%)"
			ev_set "Electric (4.1\%)"
			hy_set "Hybrid (11.0\%)"
			PanB "\textit{Observed choices}"
			tot_obs "Total"
			gas_obs "Gasoline (69.4\%)"
			diesel_obs "Diesel (24.3\%)"
			ev_obs "Electric (1.6\%)"
			hy_obs "Hybrid (4.7\%)"
			PanC "\textit{Most frequent choice}"
			gas_most "VW Polo (gas)"
			diesel_most "Ford Kuga (diesel)"
			ev_most "Renault Zoe (EV)"
			hy_most  "Toyota Yaris (Hybrid)"
             )
collabels ( "\textbf{N}" "\multicolumn{1}{c}{\makecell{\textbf{Price}}}" "\multicolumn{1}{c}{\makecell{\textbf{tax}}}"  "\multicolumn{1}{c}{\makecell{\textbf{Engine}\\ \textbf{Power}}}" 
            "\multicolumn{1}{c}{\makecell{\textbf{Weight}}}" "\multicolumn{1}{c}{\makecell{\textbf{Height}}}" "\multicolumn{1}{c}{\makecell{\textbf{Size}}}" "\multicolumn{1}{c}{\makecell{\textbf{Fuel}\\ \textbf{Costs}}}" "\multicolumn{1}{c}{\makecell{\textbf{Below}\\ \textbf{Category C}}}")
prehead("\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lccccccccc} \toprule")  
posthead("\midrule")
prefoot("\bottomrule") 
postfoot("\end{tabular*}") replace;
#delim cr







********************************************************************************
* Summary Statistics						************************************
********************************************************************************


*Create summary statistics of the observed choices (table 2)
preserve

drop if missing(choice)

*Re-generate the range5, dist_to_EV, age_group and hhsize variable: 
gen age1=(age_kw2==0)&(age_kw3==0)
gen age2=(age_kw2>0)
gen age3=(age_kw3>0)

gen hhsize=1
replace hhsize=2 if size_hh2>0
replace hhsize=3 if size_hh3>0
replace hhsize=4 if size_hh4>0 
replace hhsize=5 if size_hh5>0

egen dist_to_ev=max(ev_dist_ev), by(id)
egen range5=max(ev_range5), by(id)

keep if choice==1

set more off
matrix Tab2_tbl = J(11,5,.) 
		local count=1
		foreach v of varlist TOT_EINK wealth hhsize age1 age2 age3 range5 dist_to_ev price drive_cost et_co2  {
		format `v' %9.2f
		sum `v', d
		matrix Tab2_tbl[`count',1] = r(mean) 
		matrix Tab2_tbl[`count',2] = r(sd) 
		matrix Tab2_tbl[`count',3] = r(min) 
		matrix Tab2_tbl[`count',4] = r(p50)
		matrix Tab2_tbl[`count',5] = r(max) 
		local count=`count'+1
			
	}
matrix colnames Tab2_tbl =  Mean Stddev  min Median max
matrix rownames Tab2_tbl =  Income Wealth hhsize age1 age2 age3 range5 distev price drive co2
matrix list Tab2_tbl

foreach num of numlist 1(1)11 {
foreach numb of numlist 1(1)5 {
matrix Tab2_tbl[`num',`numb']=round(Tab2_tbl[`num',`numb'],.01)
}
}

estadd matrix Tab2_tbl, replace
#delim ;
estout matrix(Tab2_tbl, fmt(%9.0gc)) using "main/sum_total.tex", 
style(tex)  mlabels(,none) cells(e(Tab2_tbl)(fmt(%9.2f)))
varlabels (  Income      "Income (TCHF)"
             Wealth         "Wealth (TCHF)" 
			 hhsize			"Household size"
			 age1       "Age between 18 and 40"
			 age2            "Age between 40 and 60"
			 age3         "Age above 60" 
			 range5       "Public charging within 5 KM (Nb.)"
			 distev        "Distance to EV (KM)"
			 price 			"Price (TCHF)"
			 drive  "Fuel Costs (CHF/100km)"
			 co2  "CO_{2} emission (g/km)"
             )
collabels ( "\textbf{Mean}" "\textbf{Sd}"  "\textbf{Min.}" 
            "\textbf{Median}" "\textbf{Max.}")
prehead("\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lrrrrrr} \toprule")  
posthead("\midrule")
prefoot("\bottomrule") 
postfoot("\end{tabular*}") replace;
#delim cr
restore

*Creat summary statistics by category for Appendix tables:: 

preserve
drop if missing(choice)

*Re-generate the range5, dist_to_EV, age_group and hhsize variable: 
gen age1=(age_kw2==0)&(age_kw3==0)
gen age2=(age_kw2>0)
gen age3=(age_kw3>0)

gen hhsize=1
replace hhsize=2 if size_hh2>0
replace hhsize=3 if size_hh3>0
replace hhsize=4 if size_hh4>0 
replace hhsize=5 if size_hh5>0

egen dist_to_ev=max(ev_dist_ev), by(id)
egen range5=max(ev_range5), by(id)


keep if choice==1 & drivetype==1

matrix Tab3_tbl = J(11,5,.) 
		local count=1
		foreach v of varlist TOT_EINK wealth hhsize age1 age2 age3 range5 dist_to_ev price drive_cost et_co2 {
		format `v' %9.2f
		sum `v', d
		matrix Tab3_tbl[`count',1] = r(mean) 
		matrix Tab3_tbl[`count',2] = r(sd) 
		matrix Tab3_tbl[`count',3] = r(min) 
		matrix Tab3_tbl[`count',4] = r(p50)
		matrix Tab3_tbl[`count',5] = r(max) 
		local count=`count'+1
			
	}
matrix colnames Tab3_tbl =  Mean Stddev  min Median max
matrix rownames Tab3_tbl =  Income Wealth hhsize age1 age2 age3 range5 distev price drive co2
matrix list Tab3_tbl

foreach num of numlist 1(1)11 {
foreach numb of numlist 1(1)5 {
matrix Tab3_tbl[`num',`numb']=round(Tab3_tbl[`num',`numb'],.01)
}
}

estadd matrix Tab3_tbl, replace
#delim ;
estout matrix(Tab3_tbl, fmt(%9.0gc)) using "appendix/sum_gasoline.tex", 
style(tex)  mlabels(,none) cells(e(Tab3_tbl)(fmt(%9.2f)))
varlabels (  Income      "Income (TCHF)"
             Wealth         "Wealth (TCHF)" 
			 hhsize			"Household size"
			 age1       "Age between 18 and 40"
			 age2            "Age between 40 and 60"
			 age3         "Age above 60" 
			 range5       "Public charging within 5 KM (Nb.)"
			 distev        "Distance to EV (KM)"
			 price 			"Price (TCHF)"
			 drive  "Fuel Costs (CHF/100km)"
			 co2  "CO_{2} emission (g/km)"
             )
collabels ( "\textbf{Mean}" "\textbf{Sd}"  "\textbf{Min.}" 
            "\textbf{Median}" "\textbf{Max.}")
prehead("\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lrrrrrr} \toprule")  
posthead("\midrule")
prefoot("\bottomrule") 
postfoot("\end{tabular*}") replace;
#delim cr


restore

preserve
drop if missing(choice)

*Re-generate the range5, dist_to_EV, age_group and hhsize variable: 
gen age1=(age_kw2==0)&(age_kw3==0)
gen age2=(age_kw2>0)
gen age3=(age_kw3>0)

gen hhsize=1
replace hhsize=2 if size_hh2>0
replace hhsize=3 if size_hh3>0
replace hhsize=4 if size_hh4>0 
replace hhsize=5 if size_hh5>0

egen dist_to_ev=max(ev_dist_ev), by(id)
egen range5=max(ev_range5), by(id)


keep if choice==1 & drivetype==2

matrix Tab4_tbl = J(11,5,.) 
		local count=1
		foreach v of varlist TOT_EINK wealth hhsize age1 age2 age3 range5 dist_to_ev price drive_cost et_co2 {
		format `v' %9.2f
		sum `v', d
	   	
		matrix Tab4_tbl[`count',1] = r(mean) 
		matrix Tab4_tbl[`count',2] = r(sd) 
		matrix Tab4_tbl[`count',3] = r(min) 
		matrix Tab4_tbl[`count',4] = r(p50)
		matrix Tab4_tbl[`count',5] = r(max) 
		local count=`count'+1
			
	}
matrix colnames Tab4_tbl =  Mean Stddev  min Median max 
matrix rownames Tab4_tbl =  Income Wealth hhsize age1 age2 age3 range5 distev price drive co2
matrix list Tab4_tbl

foreach num of numlist 1(1)11 {
foreach numb of numlist 1(1)5 {
matrix Tab4_tbl[`num',`numb']=round(Tab4_tbl[`num',`numb'],.01)
}
}

estadd matrix Tab4_tbl, replace
#delim ;
estout matrix(Tab4_tbl, fmt(%9.0gc)) using "appendix/sum_diesel.tex", 
style(tex)  mlabels(,none) cells(e(Tab4_tbl)(fmt(%9.2f)))
varlabels (  Income      "Income (TCHF)"
             Wealth         "Wealth (TCHF)" 
			 hhsize			"Household size"
			 age1       "Age between 18 and 40"
			 age2            "Age between 40 and 60"
			 age3         "Age above 60" 
			 range5       "Public charging within 5KM (Nb.)"
			 distev        "Distance to EV (KM)"
			 price 			"Price (TCHF)"
			 drive  "Fuel Costs (CHF/100km)"
			 co2  "CO_{2} emission (g/km)"
             )
collabels ( "\textbf{Mean}" "\textbf{Sd}"  "\textbf{Min.}" 
            "\textbf{Median}" "\textbf{Max.}")
prehead("\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lrrrrrr} \toprule")  
posthead("\midrule")
prefoot("\bottomrule") 
postfoot("\end{tabular*}") replace;
#delim cr

restore

preserve
drop if missing(choice)

*Re-generate the range5, dist_to_EV, age_group and hhsize variable: 
gen age1=(age_kw2==0)&(age_kw3==0)
gen age2=(age_kw2>0)
gen age3=(age_kw3>0)

gen hhsize=1
replace hhsize=2 if size_hh2>0
replace hhsize=3 if size_hh3>0
replace hhsize=4 if size_hh4>0 
replace hhsize=5 if size_hh5>0

egen dist_to_ev=max(ev_dist_ev), by(id)
egen range5=max(ev_range5), by(id)


keep if choice==1 & drivetype==3

matrix Tab5_tbl = J(11,5,.) 
		local count=1
		foreach v of varlist TOT_EINK wealth hhsize age1 age2 age3 range5 dist_to_ev price drive_cost et_co2  {
		format `v' %9.2f
		sum `v', d
		matrix Tab5_tbl[`count',1] = r(mean) 
		matrix Tab5_tbl[`count',2] = r(sd) 
		matrix Tab5_tbl[`count',3] = r(min) 
		matrix Tab5_tbl[`count',4] = r(p50)
		matrix Tab5_tbl[`count',5] = r(max) 
		local count=`count'+1
			
	}
matrix colnames Tab5_tbl =  Mean Stddev  min Median max
matrix rownames Tab5_tbl =  Income Wealth hhsize age1 age2 age3 range5 distev price drive co2
matrix list Tab5_tbl

foreach num of numlist 1(1)11 {
foreach numb of numlist 1(1)5 {
matrix Tab5_tbl[`num',`numb']=round(Tab5_tbl[`num',`numb'],1)
}
}

estadd matrix Tab5_tbl, replace
#delim ;
estout matrix(Tab5_tbl, fmt(%9.0gc)) using "appendix/sum_elect.tex", 
style(tex)  mlabels(,none) cells(e(Tab5_tbl)(fmt(%9.2f)))
varlabels (  Income      "Income (TCHF)"
             Wealth         "Wealth (TCHF)" 
			 hhsize			"Household size"
			 age1       "Age between 18 and 40"
			 age2            "Age between 40 and 60"
			 age3         "Age above 60" 
			 range5       "Public charging within 5KM (Nb.)"
			 distev        "Distance to EV (KM)"
			 price 			"Price (TCHF)"
			 drive  "Fuel Costs (CHF/100km)"
			 co2  "CO_{2} emission (g/km)"
             )
collabels ( "\textbf{Mean}" "\textbf{Sd}"  "\textbf{Min.}" 
            "\textbf{Median}" "\textbf{Max.}")
prehead("\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lrrrrrr} \toprule")  
posthead("\midrule")
prefoot("\bottomrule") 
postfoot("\end{tabular*}") replace;
#delim cr

restore

preserve
drop if missing(choice)

*Re-generate the range5, dist_to_EV, age_group and hhsize variable: 
gen age1=(age_kw2==0)&(age_kw3==0)
gen age2=(age_kw2>0)
gen age3=(age_kw3>0)

gen hhsize=1
replace hhsize=2 if size_hh2>0
replace hhsize=3 if size_hh3>0
replace hhsize=4 if size_hh4>0 
replace hhsize=5 if size_hh5>0

egen dist_to_ev=max(ev_dist_ev), by(id)
egen range5=max(ev_range5), by(id)


keep if choice==1 & drivetype==4

matrix Tab6_tbl = J(11,5,.) 
		local count=1
		foreach v of varlist TOT_EINK wealth hhsize age1 age2 age3 range5 dist_to_ev price drive_cost et_co2  {
		format `v' %9.2f
		sum `v', d
		matrix Tab6_tbl[`count',1] = r(mean) 
		matrix Tab6_tbl[`count',2] = r(sd) 
		matrix Tab6_tbl[`count',3] = r(p5) 
		matrix Tab6_tbl[`count',4] = r(p50)
		matrix Tab6_tbl[`count',5] = r(p95) 
		local count=`count'+1
			
	}
matrix colnames Tab6_tbl =  Mean Stddev  min Median max
matrix rownames Tab6_tbl =  Income Wealth hhsize age1 age2 age3 range5 distev price drive co2
matrix list Tab6_tbl

foreach num of numlist 1(1)11 {
foreach numb of numlist 1(1)5 {
matrix Tab6_tbl[`num',`numb']=round(Tab6_tbl[`num',`numb'],.01)
}
}

estadd matrix Tab6_tbl, replace
#delim ;
estout matrix(Tab6_tbl, fmt(%9.0gc)) using "appendix/sum_hybrid.tex", 
style(tex)  mlabels(,none) cells(e(Tab5_tbl)(fmt(%9.2f)))
varlabels (  Income      "Income (TCHF)"
             Wealth         "Wealth (TCHF)" 
			 hhsize			"Household size"
			 age1       "Age between 18 and 40"
			 age2            "Age between 40 and 60"
			 age3         "Age above 60" 
			 range5       "Public charging within 5KM (Nb.)"
			 distev        "Distance to EV (KM)"
			 price 			"Price (TCHF)"
			 drive  "Fuel Costs (CHF/100km)"
			 co2  "CO_{2} emission (g/km)"
             )
collabels ( "\textbf{Mean}" "\textbf{Sd}"  "\textbf{Min.}" 
            "\textbf{Median}" "\textbf{Max.}")
prehead("\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lrrrrrr} \toprule")  
posthead("\midrule")
prefoot("\bottomrule") 
postfoot("\end{tabular*}") replace;
#delim cr

restore 

